fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23755
fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23755bbartels wants to merge 15 commits intoanomalyco:devfrom
Conversation
|
The following comment was made by an LLM, it may be inaccurate: Potential Duplicate/Related PRs Found:
These PRs appear to be addressing overlapping issues with preserving thinking/reasoning blocks in Anthropic message transforms. The current PR (23755) consolidates fixes for three specific code paths (empty content filtering, tool-use reordering, and cache control hints) that were inadvertently modifying these blocks. |
|
@rekram1-node Any chance you could have a look? ~20 LOC of actual changes, rest is tests |
Issue for this PR
Closes #14332
Type of change
What does this PR do?
Fixes the Anthropic API error:
messages.X.content.Y: 'thinking' or 'redacted_thinking' blocks in the latest assistant message cannot be modified.The Anthropic API requires that
thinkingandredacted_thinkingblocks in assistant messages are sent back byte-for-byte identical in subsequent requests. Three code paths innormalizeMessages/applyCachinginsrc/provider/transform.tswere inadvertently modifying these blocks:redacted_thinkingblocks. The AI SDK represents these as{ type: "reasoning", text: "", providerOptions: { anthropic: { redactedData: "..." } } }— the empty text caused them to be filtered out. Fix: only filter reasoning parts that have noproviderOptions.[thinking, tool_call, text]into two separate messages, changing which message the thinking blocks belong to. Fix: skip the reorder when reasoning blocks are present.How did you verify your code works?
Added tests covering all three fixes to
test/provider/transform.test.ts. All 143 tests pass (130 existing + 13 new).bun test test/provider/transform.test.ts
143 pass, 0 fail
Screenshots / recordings
N/A - not a UI change.
Checklist